package com.bridgeintelligent.tag.bulkload.service.core.fileload;

import com.alibaba.fastjson.JSON;
import com.bridgeintelligent.tag.bulkload.api.fileload.Filter;
import com.bridgeintelligent.tag.bulkload.api.fileload.FilterGroup;
import com.bridgeintelligent.tag.constants.PublicConstant;
import com.bridgeintelligent.tag.mapper.EsTimeMapper;
import com.bridgeintelligent.tag.mapper.FileExportMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AllArgsConstructor
@Component
@Slf4j
public class ConditionBuilder {

    private static final String PARENT_ID = "-";
    private FileExportMapper fileExportMapper;
    private EsTimeMapper esTimeMapper;

    public List<FilterGroup> findFilterGroup(String parentId, String customersId, List<Filter> filters) {
        Map<String, String> fgmap = new HashMap<>(16);
        fgmap.put("parentId", parentId);
        fgmap.put("customers_id", customersId);
        List<FilterGroup> filterGroupList = fileExportMapper.findFilterGroup(fgmap);
        Filter filter;
        List<Filter> filterList;
        if (filterGroupList != null && !filterGroupList.isEmpty()) {
            for (FilterGroup filterGroup : filterGroupList) {
                filterList = new ArrayList<>();
                List<String> filterIds = fileExportMapper.findFilterIds(filterGroup.getId());
                if (filterIds != null && !filterIds.isEmpty()) {
                    for (String filterId : filterIds) {
                        filter = fileExportMapper.findOneFilterById(filterId);
                        filter.setValueList(JSON.parseArray(filter.getValueListStr(), String.class));
                        filterList.add(filter);
                        filters.add(filter);
                    }
                }
                filterGroup.setFilters(filterList);
                List<FilterGroup> list = findFilterGroup(filterGroup.getId(), customersId, filters);
                if (list != null && !list.isEmpty()) {
                    filterGroup.setChildren(list);
                }
            }
        }
        return filterGroupList;
    }
    public List<FilterGroup> findFilterGroupAndOverwriteDataDate(String parentId, String customersId, List<Filter> filters) {
        Map<String, String> fgmap = new HashMap<>(16);
        fgmap.put("parentId", parentId);
        fgmap.put("customers_id", customersId);
        List<FilterGroup> filterGroupList = fileExportMapper.findFilterGroup(fgmap);
        Filter filter;
        List<Filter> filterList;
        if (filterGroupList != null && !filterGroupList.isEmpty()) {
            for (FilterGroup filterGroup : filterGroupList) {
                filterList = new ArrayList<>();
                List<String> filterIds = fileExportMapper.findFilterIds(filterGroup.getId());
                if (filterIds != null && !filterIds.isEmpty()) {
                    for (String filterId : filterIds) {
                        filter = fileExportMapper.findOneFilterById(filterId);
                        //重置DataDate
                        filter.setDataDate(this.overwriteDataDate(filter.getDataDate(),filter.getIndexId()));
                        filter.setValueList(JSON.parseArray(filter.getValueListStr(), String.class));
                        filter.setTimeType(false);
                        filterList.add(filter);
                        filters.add(filter);
                    }
                }
                filterGroup.setFilters(filterList);
                List<FilterGroup> list = findFilterGroupAndOverwriteDataDate(filterGroup.getId(), customersId, filters);
                if (list != null && !list.isEmpty()) {
                    filterGroup.setChildren(list);
                }
            }
        }
        return filterGroupList;
    }

    private String overwriteDataDate(String dataDate,String indexId){
        //如果是八位日期或者0 拼0 如果是六位日期拼后缀
        if(PublicConstant.ZERO.equals(dataDate)|| (PublicConstant.pattern.matcher(dataDate).matches() && dataDate.length() == PublicConstant.LENGTH8 )){
            return PublicConstant.ZERO;
        }else{
            List<String>  staticDataDates = esTimeMapper.findSymbolByDate(indexId,dataDate);
            return CollectionUtils.isNotEmpty(staticDataDates)?staticDataDates.get(0):dataDate;
        }
    }
}
